import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import AutoImport from 'unplugin-auto-import/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import ElementPlus from 'unplugin-element-plus/vite'

import Unocss from 'unocss/vite'
import {
  presetAttributify,
  presetIcons,
  presetUno,
  transformerDirectives,
  transformerVariantGroup,
} from 'unocss'

const pathSrc = path.resolve(__dirname, 'src')

var obj = {
  base: ''
}
const target = process.env.npm_lifecycle_event;
if (target == 'build') {
//测试
  var obj = {
    base: '/admin',
    }
} else {
  obj = {
    base: '',
  }
}

// https://vitejs.dev/config/
export default defineConfig({
  base: obj.base,
  server: {
    port: 3000,
    open: true, //自动打开
    //base: "./ ", //生产环境路径
    proxy: { // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
      // 正则表达式写法
      '/api/admin': {
        target: 'http://localhost:5012/', // 后端服务实际地址
        changeOrigin: true, //开启代理
        rewrite: (path) => path.replace(/^\/api\/admin/, '')
      },
      '/api/novel': {
        target: 'http://localhost:5016', // 后端服务实际地址
        changeOrigin: true, //开启代理
        // rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/api/zhongyi': {
        target: 'http://localhost:5018/', // 后端服务实际地址
        changeOrigin: true, //开启代理
        rewrite: (path) => path.replace(/^\/api\/zhongyi/, '')
      },
      '/api/file': {
        target: 'http://localhost:5013', // 后端服务实际地址
        changeOrigin: true, //开启代理
        // rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@/': `${pathSrc}/`,
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
  plugins: [
    vue(),
    AutoImport({
      imports: ['vue', 'vue-router', 'vue-i18n', '@vueuse/head', '@vueuse/core', 'pinia'],
      dirs: ['./store', './api/spider'],
      // 可以选择auto-import.d.ts生成的位置，使用ts建议设置为'src/auto-import.d.ts'
      // dts: 'src/auto-import.d.ts'
      // resolvers: [ElementPlusResolver()],
    }),
    Components({
      // allow auto load markdown components under `./src/components/`
      extensions: ['vue', 'md'],
      // allow auto import and register components used in markdown
      include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
        IconsResolver({
          // 自动引入的Icon组件统一前缀，默认为 i，设置false为不需要前缀
                prefix: 'icon',
                // 当图标集名字过长时，可使用集合别名
                alias: {
                  system: 'system-uicons'
                },
              })
      ],
      dts: 'src/components.d.ts',
      dirs: ['src/view'],
      directoryAsNamespace: true
    }),
    Icons({
      // experimental
      autoInstall: true,
      compiler: 'vue3',
    }),
    ElementPlus({
      useSource: true
    }),
    // https://github.com/antfu/unocss
    // see unocss.config.ts for config
    Unocss({
      presets: [
        presetUno(),
        presetAttributify(),
        presetIcons({
          scale: 1.2,
          warn: true,
        }),
      ],
      transformers: [
        transformerDirectives(),
        transformerVariantGroup(),
      ]
    }),
  ],
  build: {
    outDir: 'admin',
  
  }
})
